package com.ui.basic.service.administrative;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.assembler.administrative.ExchangeRateAssembler;
import com.ui.basic.command.administrative.CommandExchangeRate;
import com.ui.basic.ibatis.dao.admin.TblAExchangeRateDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCurrenciesDAO;
import com.ui.basic.ibatis.model.admin.TblAExchangeRate;
import com.ui.basic.ibatis.model.admin.TblAExchangeRateExample;
import com.ui.basic.ibatis.model.masterdata.TblMCurrencies;
import com.ui.basic.ibatis.model.masterdata.TblMCurrenciesExample;
import com.ui.basic.service.report.LogExchangeRateService;

@Repository("exchangeRateService")
public class ExchangeRateServiceImpl implements ExchangeRateService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
	.getLog(ExchangeRateServiceImpl.class);
	
	private TblAExchangeRateDAO tblAExchangeRateDAO;
	private TblMCurrenciesDAO tblMCurrenciesDAO;
	private LogExchangeRateService logExchangeRateService;
	
	@Autowired
	public void setTblAExchangeRateDAO(TblAExchangeRateDAO tblAExchangeRateDAO) {
		this.tblAExchangeRateDAO = tblAExchangeRateDAO;
	}
	
	@Autowired
	public void setTblMCurrenciesDAO(TblMCurrenciesDAO tblMCurrenciesDAO) {
		this.tblMCurrenciesDAO = tblMCurrenciesDAO;
	}
	
	@Autowired
	public void setLogExchangeRateService(
			LogExchangeRateService logExchangeRateService) {
		this.logExchangeRateService = logExchangeRateService;
	}
	
	@Transactional
	public void setupNewCurrency(TblMCurrencies tblMCurrencies) throws SQLException {
		LOGGER.debug(">>>ExchangeRateServiceImpl.setupNewCurrency");
		
		tblMCurrenciesDAO.insert(tblMCurrencies);
		
		List<TblMCurrencies> currList = null;
		TblMCurrenciesExample example = new TblMCurrenciesExample();
		example.createCriteria().andCurrabrevEqualTo(tblMCurrencies.getCurrabrev());
		
		tblMCurrencies = (TblMCurrencies) tblMCurrenciesDAO.selectByExample(example).get(0);
		
		example.clear();
		example.createCriteria().andIdNotEqualTo(tblMCurrencies.getId());
		currList = tblMCurrenciesDAO.selectByExample(example);
		
		if(currList != null){
			for(TblMCurrencies curr: currList){
				TblAExchangeRate tblAExchangeRate = new TblAExchangeRate();
				tblAExchangeRate.setSrcCurrId(tblMCurrencies.getId());
				tblAExchangeRate.setDestCurrId(curr.getId());
				tblAExchangeRate.setRate(new Double(1));
				tblAExchangeRate.setCommission(new Double(0));
				tblAExchangeRate.setCreatedby("SYSTEM");
				tblAExchangeRate.setCreateddate(new Date());
				
				tblAExchangeRateDAO.insert(tblAExchangeRate);

				tblAExchangeRate.setSrcCurrId(curr.getId());
				tblAExchangeRate.setDestCurrId(tblMCurrencies.getId());
				
				tblAExchangeRateDAO.insert(tblAExchangeRate);
			}
		}
		
		
	}
	
	@Transactional(readOnly = true)
	public CommandExchangeRate getExchangeRate(
			CommandExchangeRate commandExchangeRate) throws SQLException {
		LOGGER.debug(">>>ExchangeRateServiceImpl.getExchangeRate");
		if (commandExchangeRate != null) {
			TblAExchangeRateExample example = new TblAExchangeRateExample();

			int rec = 0;
			try {
				rec = tblAExchangeRateDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandExchangeRate.calculate(rec);
			example.setIntRowNum((commandExchangeRate.getPgIndex() - 1)
					* commandExchangeRate.getPgRows());
			example.setIntRowsPerPage(commandExchangeRate.getPgRows());
			example.setOrderByClause("SRC_CURR_ID");
			commandExchangeRate.setData(tblAExchangeRateDAO.selectByExample(example));
		}

		return commandExchangeRate;
	}
	
	@Transactional(readOnly = true)
	public void setCombo(CommandExchangeRate commandExchangeRate)
			throws SQLException {
		LOGGER.debug(">>>ExchangeRateServiceImpl.setCombo");

		TblMCurrenciesExample currenciesExample = new TblMCurrenciesExample();
		currenciesExample.setOrderByClause("currabrev");
		commandExchangeRate.setCurrList(ComboBoxPopulator.populate(
				tblMCurrenciesDAO.selectByExample(currenciesExample), "id",
				new String[] { "currabrev", "currency" }, "-"));

		
	}
	
	@Transactional(readOnly = true)
	public CommandExchangeRate getExchangeRateDetail(
			CommandExchangeRate commandExchangeRate) throws SQLException {
		LOGGER.debug(">>>ExchangeRateServiceImpl.getExchangeRateDetail");
		commandExchangeRate.setProperty(tblAExchangeRateDAO.selectByPrimaryKey(commandExchangeRate.getId()));
		return commandExchangeRate;
	}
	
	@Transactional
	public void save(CommandExchangeRate commandExchangeRate)
			throws SQLException {
		LOGGER.debug(">>>ExchangeRateServiceImpl.getExchangeRateDetail");
		ExchangeRateAssembler assembler = new ExchangeRateAssembler();
		TblAExchangeRate tblAExchangeRate = assembler.toEntity(commandExchangeRate);
		
		if (commandExchangeRate.getId() != null) {
			tblAExchangeRate.setCreatedby((String) (SecurityUtils.getSubject()
					.getSession().getAttribute(Globals.CURRENT_USER_NAME)));
			tblAExchangeRate.setCreateddate(new Date());
			try {
				//tblAExchangeRateDAO.updateByPrimaryKeySelective(tblAExchangeRate);
				logExchangeRateService.logUpdateRate(tblAExchangeRate);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		} else {
			throw new SQLException("Never Add Exchanged Rate Manually");
		}		
	}
}